package org.airline.action;

import javax.annotation.Resource;

import org.airline.common.IModuleName;
import org.airline.entity.Notice;
import org.airline.entity.PageBean;
import org.airline.service.NoticeService;
import org.springframework.stereotype.Component;

@Component("NoticeAction")
@SuppressWarnings("all")
public class NoticeAction extends SuperAction {

	private int page = 1;
	private int pageSize = 10;
	private String forwardURL;
	private IModuleName iModuleName;
	private String keyWord;
	private PageBean pageBean;
	private Notice notice;
	private NoticeService noticeService;

	// 添加公告信息
	public String addNoticeInfo() throws Exception {
		try {
			noticeService.saveNotice(notice);
		} catch (Exception e) {
			logFailureString(iModuleName.ADD, iModuleName.ADD, "添加公告失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.ADD, iModuleName.ADD, "添加公告成功");
		forwardURL = "NoticeInfo!getNoticeInfo?page=1";
		return "notice";
	}

	// 后台查询所有公告信息并分页显示
	public String getNoticeInfo() throws Exception {
		try {
			pageBean = noticeService.queryForPage(pageSize, page);
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "获取所有公告失败！", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "获取所有公告成功");
		forwardURL = "/admin/showAllNotice.jsp";
		return SUCCESS;
	}

	// 前台分页显示所有公告
	public String getAllNoticeInfo() throws Exception {
		try {
			pageBean = noticeService.queryForPage(pageSize, page);
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "获取所有公告失败！", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "获取所有公告成功");
		forwardURL = "/jsp/showNewstNotice.jsp";
		return SUCCESS;
	}
	
	// 根据ID查询公告
	public String getNoticeInfoById() throws Exception {
		try {
			notice = noticeService.getNoticeById(notice.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据ID获取公告", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据ID获取公告");
		forwardURL = "/admin/updateNotice.jsp";
		return SUCCESS;
	}

	// 修改公告信息
	public String updateNoticeInfo() throws Exception {
		try {
			noticeService.updateNotice(notice);
		} catch (Exception e) {
			logFailureString(iModuleName.UPDATE, iModuleName.UPDATE, "修改公告失败",
					e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.UPDATE, iModuleName.UPDATE, "修改公告成功");
		forwardURL = "NoticeInfo!getNoticeInfo?page=1";
		return "notice";
	}

	// 删除公告信息
	public String deleteNoticeInfo() throws Exception {
		try {
			noticeService.deleteNotice(notice.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.DELETE, iModuleName.DELETE,
					"根据ID删除公告失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.DELETE, iModuleName.DELETE, "根据ID删除公告成功");
		forwardURL = "NoticeInfo!getNoticeInfo?page=1";
		return "notice";
	}
	
	// 根据关键字分页查询公告信息
	public String getNoticesByKeyWords() throws Exception {
		try {
			pageBean = noticeService.queryForPageByKeyWords(keyWord, pageSize,
					page);
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET,
					"根据关键字分页查询公告信息失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET,
				"根据关键字分页查询公告信息成功");
		forwardURL = "/admin/showAllNotice.jsp";
		return SUCCESS;
	}
	
	// 根据公告ID获取公告信息
	public String getNoticeInfoByID() throws Exception {
		try {
			notice = noticeService.getNoticeById(notice.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据标题获取公告信息失败",
					e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据标题获取公告信息成功");
		forwardURL = "/jsp/showNotice.jsp";
		return SUCCESS;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getForwardURL() {
		return forwardURL;
	}

	public void setForwardURL(String forwardURL) {
		this.forwardURL = forwardURL;
	}

	public IModuleName getiModuleName() {
		return iModuleName;
	}

	public void setiModuleName(IModuleName iModuleName) {
		this.iModuleName = iModuleName;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	@Resource(name = "NoticeService")
	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

}
